package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.WebMessageDao;
import ysq.xyg.model.WebMessage;
import ysq.xyg.service.WebMessageService;

import javax.annotation.Resource;
import java.util.List;


@Service("webMessageService")
public class WebMessageServiceImpl extends BaseServiceImpl<WebMessage> implements WebMessageService {

    @Resource
    private WebMessageDao webMessageDao;
    @Autowired
    public void setpictureDao(WebMessageDao webMessageDao) {
        this.baseDao = webMessageDao;
    }

    @Override
    public PageInfo<WebMessage> page(WebMessage webMessage, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        return new PageInfo<WebMessage>(list(webMessage));
    }

    @Override
    public List<WebMessage> list(WebMessage webMessage) {
        // 通用Example查询
        Example example = new Example(WebMessage.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotEmpty(webMessage.getType())) {
            criteria.andEqualTo("type", webMessage.getType());
        }
        if (StringUtils.isNotEmpty(webMessage.getTitle())) {
            criteria.andLike("title", "%"+webMessage.getTitle()+"%");
        }
        if (StringUtils.isNotEmpty(webMessage.getEmail())) {
            criteria.andLike("email", "%"+webMessage.getEmail()+"%");
        }
        example.setOrderByClause(" create_time desc");
        List<WebMessage> list = webMessageDao.selectByExample(example);
        return list;
    }
}
